package com.woniu.controller;

import com.ruoyi.common.utils.token.AccToken;
import com.ruoyi.common.utils.token.JwtTokenUtils;
import com.woniu.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 杨光 微信号:tolryg
 * @time 11:16
 */
@RestController
@CrossOrigin
public class FileUploadController {


    @Value("${server.port}")
    private String port;
    @Autowired
    private FileUploadService fileUploadService;

    @RequestMapping("/file/upload")
    public String upload(MultipartFile file, HttpServletRequest request){
        System.out.println("port="+this.port);
        String authorization = request.getHeader("Authorization");
        System.out.println("header=Authorization="+request.getHeader("Authorization"));
        AccToken accToken = null;
        if(authorization.length()>7){
            accToken = JwtTokenUtils.parseWebToken(authorization.substring(7));
        }
        if(accToken == null){
            return "error:未登录";
        }

        String url = fileUploadService.upload(file);
        return url;
    }
}
